function sortPicListByArr2(arr1, arr2) {
  return arr1.map((item) => {
    const matchItem = arr2.find((i) => i._id === item._id);
    if (matchItem) {
      const newPicList = [];
      matchItem.piclist.forEach((picId) => {
        const targetPic = item.piclist.find((pic) => pic._id === picId);
        if (targetPic) {
          newPicList.push(targetPic);
        }
      });
      item.piclist = newPicList;
    }
    return item;
  });
}
const dayjs = require('dayjs');

module.exports = {
  _before: function () {
    // 通用预处理器
  },
  async add(params = {}) {
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    return await dbJQL.collection('dc-activity-everyday').add(params);
  },
  async list({ pageSize = 5, currentPage = 1 } = {}) {
    pageSize = Math.min(30, pageSize);
    let skip = (currentPage - 1) * pageSize;
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    let dayTemp = dbJQL
      .collection('dc-activity-everyday')
      .orderBy('day desc')
      .skip(skip)
      .limit(pageSize)
      .getTemp();
    let picTemp = dbJQL
      .collection('dc-bizhi-piclist')
      .field('_id,picurl')
      .getTemp();
    let result = await dbJQL.collection(dayTemp, picTemp).get({
      getCount: true,
    });
    let lists = await dbJQL
      .collection('dc-activity-everyday')
      .orderBy('day desc')
      .skip(skip)
      .limit(pageSize)
      .get();
    let data = sortPicListByArr2(result.data, lists.data);
    data.forEach((item) => {
      let find = lists.data.find((i) => i._id === item._id);
      item.length = find.piclist.length;
    });
    return {
      ...result,
      data,
    };
  },
  async remove(id = '') {
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });

    return await dbJQL.collection('dc-activity-everyday').doc(id).remove();
  },
  async item(id = '') {
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    let dayTemp = dbJQL
      .collection('dc-activity-everyday')
      .where(`_id=='${id}'`)
      .orderBy('createTime desc')
      .limit(1)
      .getTemp();
    let picTemp = dbJQL
      .collection('dc-bizhi-piclist')
      .field('_id,picurl')
      .getTemp();
    let result = await dbJQL.collection(dayTemp, picTemp).get();
    let lists = await dbJQL
      .collection('dc-activity-everyday')
      .where(`_id=='${id}'`)
      .orderBy('createTime desc')
      .limit(1)
      .get();
    let data = sortPicListByArr2(result.data, lists.data);
    return {
      ...result,
      data,
    };
  },
  async update(params = {}) {
    let { _id, ...data } = params;
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    return await dbJQL.collection('dc-activity-everyday').doc(_id).update(data);
  },
  async _timing() {
    let today = dayjs().format('YYYY-MM-DD');
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    dbJQL.setUser({
      uid: '67ba8f260af6d47c0e2a64a5',
    });

    let { total } = await dbJQL
      .collection('dc-activity-everyday')
      .where(`day == "${today}"`)
      .count();
    if (total != 0) {
      console.log({ msg: '已有数据,无需自动新增' });
      return { msg: '已有数据' };
    }
    let { data } = await dbJQL
      .collection('dc-bizhi-piclist')
      .aggregate()
      .match({ checked: true })
      .sample({ size: 15 })
      .limit(15)
      .project({
        _id: 1,
      })
      .end();
    let picList = data.map((item) => item._id);
    let result = await dbJQL.collection('dc-activity-everyday').add({
      piclist:picList,
      day: today,
      theme: '每日精选推荐',
    });
    console.log(result);
    return result;
  },
};
